const BitShares = require('btsdex')
const cache = require('./cache');
const format = require('string-format')
//const bot = require('./handlers/tgBot')
const mnMonitor = require('./mnMonitor')

const transferCoinHandler = require('./handlers/transferCoinToCBHandler')
const orderCreatedHandler = require('./handlers/orderCreatedHandler')


format.extend(String.prototype, {})

console.log("{0} running...".format("System"))

BitShares.init('wss://api.bts.ai')
BitShares.connect()
BitShares.subscribe('connected', functionToCall)

const task = mnMonitor.startMNStatusMonitor()

async function functionToCall(obj) {

    BitShares.subscribe('block', blockSubscribeCallback)

    // blockC = await BitShares.db.get_block(31606771)
    // blockC.transactions.forEach(async trans => {
    //     orderCreatedHandler(trans)
    // })
}

async function blockSubscribeCallback(obj) {

    //asset = await BitShares.assets.id("1.3.4381").precision

    block = await BitShares.db.get_block(obj[0].head_block_number)
    transacations = block.transactions

    transacations.forEach(async trans => {
        await transferCoinHandler(trans)
        await orderCreatedHandler(trans)
    });
}
